package com.example.admin.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.admin.R;
import com.example.admin.SQLite.Complaint;
import com.example.admin.SQLite.SqLiteOpenHelper;
import com.example.admin.SQLite.TongZhi;
import com.example.admin.SQLite.dao.TongZhiDao;
import com.example.admin.activity.NoticeDetailActivity;
import com.example.admin.activity.NoticeDetailActivitys;
import com.example.admin.adapter.AdminTab1dapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class Tab1Fragment extends Fragment {

    TongZhiDao tongZhiDao ;
    AdminTab1dapter tab1dapter;
    RecyclerView recyclerView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_tab1, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tongZhiDao = new TongZhiDao(getActivity());
        recyclerView = view.findViewById(R.id.recyclerView);
        tab1dapter = new AdminTab1dapter();
        recyclerView.setAdapter(tab1dapter);
        tab1dapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                startActivity(new Intent(getActivity(), NoticeDetailActivitys.class)
                        .putExtra("bean", tab1dapter.getItem(position)));
            }
        });
        tab1dapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("是否执行此操作");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        tongZhiDao.DeleteContacts(tab1dapter.getItem(position).id);
                        tab1dapter.remove(position);
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });
        view.findViewById(R.id.tvSure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                View views = getLayoutInflater().inflate(R.layout.dialog_tab1, null);
                builder.setView(views);
                AlertDialog dialog = builder.create();
                dialog.show();
                EditText tvTitle = views.findViewById(R.id.tvTitle);
                EditText tvCont = views.findViewById(R.id.tvCont);
                views.findViewById(R.id.tvSure).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String tit = tvTitle.getText().toString().trim();
                        String cont = tvCont.getText().toString().trim();
                        Date currentTime = new Date();
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        String time= format.format(currentTime);
                        dialog.dismiss();
                        TongZhi bean = new TongZhi();
                        bean.tongzhiTitle = tit;
                        bean.tongzhiContent = cont;
                        bean.tongzhiTime = time;
                        tongZhiDao.addData(bean);
                        getdata();
                    }
                });
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        getdata();
    }

    private void getdata() {
        ArrayList<TongZhi> list = tongZhiDao.FindWhereData();
        Log.e("的收费收费的方式", "getdata: "+list.size() );
        tab1dapter.setNewData(list);
    }
}